package com.api.workflow.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;
import com.api.workflow.util.YNHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 非生产物资验收
 * */
public class YNMMI023Action extends BaseBean implements Action {

    public static void main(String[] args) {

    }

    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableName = "formtable_main_" + formidAbs;

        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }
        baseBean.writeLog("非生产物资验收=======mainTableInfoMap:" + com.alibaba.fastjson.JSONObject.toJSONString(mainTableInfoMap));

        //明细表数据
        List<Map<String, Object>> detailTableMap = getMaps(requestInfo);
        baseBean.writeLog("非生产物资验收=======detailTableMap:" + com.alibaba.fastjson.JSONObject.toJSONString(detailTableMap));

        try {

            JSONObject jsonObject = new JSONObject();
            String sfsb = String.valueOf(mainTableInfoMap.get("sfsb"));
            String sfkclys = String.valueOf(mainTableInfoMap.get("sfkclys"));
            if (sfkclys.equals("0") && sfsb.equals("1")) {
                return Action.SUCCESS;
            }
            jsonObject.put("ZOAMID", mainTableInfoMap.get("zoamid"));
            jsonObject.put("BUDAT", mainTableInfoMap.get("budat"));
            jsonObject.put("BKTXT", mainTableInfoMap.get("bktxt"));
            jsonObject.put("EBELN", mainTableInfoMap.get("ebeln"));
            JSONArray jsonArray = new JSONArray();
            for (Map<String, Object> map : detailTableMap) {
                JSONObject items = new JSONObject();
                items.put("EBELP", map.get("ebelp"));
                items.put("MENGE", map.get("menge"));
                jsonArray.add(items);

            }

            jsonObject.put("ITEMS", jsonArray);
            baseBean.writeLog("wyq===MMI023非生产物资验收接口===jsonObject" + jsonObject.toString());
            String post = YNHttpUtils.MMI023(jsonObject.toString(), YNHttpUtils.token());
            baseBean.writeLog("wyq===MMI023非生产物资验收接口===post" + post);


            //更新主表
            RecordSet recordSet = new RecordSet();
            String updateMainTableName = "update " + tableName + " set  qqcs = ? , fhjg = ?  where  requestid = ? ";
            recordSet.executeUpdate(updateMainTableName, jsonObject.toString(), post, requestid);


            JSONObject object = JSONObject.parseObject(post);
            String status = object.getString("STATUS");
            String message = object.getString("MESSAGE");
            if (!status.equals("S")) {
                requestInfo.getRequestManager()
                        .setMessageid(requestInfo.getRequestid() + "OA-MMI023非生产物资验收接口");
                requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:" + message);
                return Action.FAILURE_AND_CONTINUE;
            }


        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager()
                    .setMessageid(requestInfo.getRequestid() + "OA-MMI023非生产物资验收接口");
            requestInfo.getRequestManager().setMessagecontent("接口异常" + e.toString());
            return Action.FAILURE_AND_CONTINUE;

        }


        return Action.SUCCESS;
    }

    /**
     * 提取明细表全部的数据
     *
     * @param requestInfo
     * @return
     */
    private static List<Map<String, Object>> getMaps(RequestInfo requestInfo) {
        DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
        //这里是存储了全部的明细表数据  可自行修改存储结构
        List<Map<String, Object>> detailTableMap = new ArrayList<>();
        if (detailTable.length > 0) {
            for (int i = 0; i < detailTable.length; i++) {
                DetailTable dt = detailTable[i];
                Row[] s = dt.getRow();
                for (int j = 0; j < s.length; j++) {
                    Row r = s[j];
                    Cell c[] = r.getCell();
                    Map<String, Object> objectObjectHashMap = new HashMap<>();
                    for (int k = 0; k < c.length; k++) {
                        Cell c1 = c[k];
                        String name = c1.getName().toLowerCase();
                        String value = c1.getValue();
                        objectObjectHashMap.put(name, value);
                    }
                    detailTableMap.add(objectObjectHashMap);
                }
            }
        }
        return detailTableMap;
    }

}

